package org.tigris.atlas.examples.service;

import org.tigris.atlas.examples.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.examples.service.delegate.SimpleDomainServiceDelegate;
import org.tigris.atlas.examples.transfer.SimpleDomain;
import org.tigris.atlas.examples.transfer.TransferObjectFactory;

import java.util.Collection;
import java.util.Iterator;

import junit.framework.TestCase;

public class TestSimpleDomainService extends TestCase {

	SimpleDomainServiceDelegate delegate;
	EntityMaintenanceServiceDelegate ems;

	protected void setUp() throws Exception {
		super.setUp();
					
		delegate = ServiceDelegateFactory.createSimpleDomainServiceDelegate();
		ems = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
				
		Iterator i = delegate.selectAllSimpleDomains().getSimpleDomains().iterator();
		while( i.hasNext() ) {
			SimpleDomain sd = (SimpleDomain) i.next();
			ems.delete( sd.getSimpleDomainPK() );
		}
		
		SimpleDomain sd1 = TransferObjectFactory.createSimpleDomain();
		sd1.setName( "Name1" );
		sd1.setType( "A" );
		ems.save( sd1 );
		
		SimpleDomain sd2 = TransferObjectFactory.createSimpleDomain();
		sd2.setName( "Name2" );
		sd2.setType( "B" );
		ems.save( sd2 );
		
		SimpleDomain sd3 = TransferObjectFactory.createSimpleDomain();
		sd3.setName( "Name3" );
		sd3.setType( "B" );
		ems.save( sd3 );
	}

	protected void tearDown() throws Exception {
		Iterator i = delegate.selectAllSimpleDomains().getSimpleDomains().iterator();
		while( i.hasNext() ) {
			SimpleDomain sd = (SimpleDomain) i.next();
			ems.delete( sd.getSimpleDomainPK() );
		}
		
		super.tearDown();
	}
	
	public void testDoSomething() throws Exception {
		delegate.doSomething();
		assertTrue(true);
	}
	
	public void testSelectAllSimpleDomainsByType() throws Exception {	
		Collection items = delegate.selectAllSimpleDomainsByType("A").getSimpleDomains();
		assertNotNull(items);
		assertEquals(1, items.size());
		
		items = delegate.selectAllSimpleDomainsByType("B").getSimpleDomains();
		assertNotNull(items);
		assertEquals(2, items.size());
	}
	
	public void testSelectAllSimpleDomains() throws Exception {
		Collection items = delegate.selectAllSimpleDomains().getSimpleDomains();
		
		assertNotNull(items);
		assertEquals(3, items.size());
	}
	
}
